﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NET = System.Net;
using SOCK = System.Net.Sockets;

namespace ChatProgram
{
    internal class SocketCommunicator
    {
        private TcpClient client;

        private int port = 2000;
        public SocketCommunicator()
        {
            try
            {

                client = new TcpClient();
                bool Invalid = true;
                NET.IPAddress ipAddress = null;
                do
                {
                    string ip = SpecifyIP();
                    try
                    {
                        client.Connect(ip, port);
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine("incorrect ip");
                    }
                } while (!client.Connected);
                Console.WriteLine("Accepted");
                Console.WriteLine("Connected");
                Thread thread = new Thread(new ThreadStart(Run));
                thread.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Disconnected from server");
                Console.ReadLine();
            }
        }
        private string SpecifyIP()
        {
            string ip = "";
            try
            {
                Console.WriteLine("Enter the ip of your partner");
                ip = Console.ReadLine();
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid");
            }
            return ip;
        }
        private void ConnectToIP(string ip)
        {
            try
            {
                client.Connect(ip, port);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private StreamReader reader;
        private void ReadForInfo()
        {
            reader = new StreamReader(client.GetStream());
            while(client.Connected)
            {
                try
                {
                    if (client.GetStream().DataAvailable)
                    {
                        Console.WriteLine(reader.ReadLine());
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
                catch(Exception)
                {
                }
            }
        }
        private void Run()
        {
            Thread thread = new Thread(new ThreadStart(ReadForInfo));
            thread.Start();
            Thread inputAsker = new Thread(new ThreadStart(AskForInput));
            inputAsker.Start();
        }
        private void AskForInput()
        {
            while (client.Connected)
            {
                try
                {
                    StreamWriter writer = new StreamWriter(client.GetStream());
                    if (!client.GetStream().DataAvailable)
                    {
                        string info = Console.ReadLine();
                        if (!info.IsNullEmptyOrWhiteSpace())
                        {
                            //dataToSend = info;
                            writer.WriteLine(info);
                            writer.Flush();
                        }
                        else
                            Console.WriteLine("Enter something valid");
                    }

                }
                catch (Exception)
                {
                }
            }
        }

    }

    //public static void Receive(Socket socket,)
    }